﻿@model IPagedList<Student>
@{
    ViewData["Title"] = "Students";
    Affiliation affiliation = ViewBag.Affiliation;
    IReadOnlyList<VerifyCode> codes = ViewBag.Codes;
    var prefix = affiliation.Id + "_";
}

<h2 class="mt-2 mb-3">@ViewData["Title"] in @affiliation.Name</h2>

<div class="alert alert-primary" style="max-width:35em">
    <razor asp-show-if="codes.Count == 0">
        Currently you have no student invitation code.
        <a class="alert-link" asp-controller="Tenants" asp-action="CreateVerifyCode" asp-route-returnUrl="@ViewData["RefreshUrl2"]" data-toggle="ajaxWindow">Create one &raquo;</a>
    </razor>
    <razor asp-show-if="codes.Count > 0">
        You can share verify codes to the unverified students.
        They can use this code to verify their student identity.
        <ul class="mb-0 mt-2">
            @foreach (var code in codes)
            {
            <li>
                <b style="font-family:Consolas,monospace">@code.Code</b>
                : Used @code.RedeemCount times,
                created since @code.CreationTime.ToString("d"),
                <a asp-controller="Tenants" asp-action="InvalidateVerifyCode" asp-route-code="@code.Code" asp-route-returnUrl="@ViewData["RefreshUrl2"]" data-toggle="ajaxWindow">invalidate</a>
            </li>
            }
        </ul>
    </razor>
</div>

<div class="d-none" id="paging">
    <pagination bs-current-page="Model.CurrentPage" bs-total-page="Model.TotalPage" />
</div>

<div class="table-wrapper">
    <table class="data-table table table-sm table-striped" style="width:auto">
        <thead>
            <tr role="row">
                <th style="min-width:5em" scope="col" class="sortable searchable">ID</th>
                <th style="min-width:4em" scope="col" class="sortable searchable">name</th>
                <th style="min-width:5em" scope="col" class="sortable searchable">user</th>
                <th style="min-width:10em" scope="col" class="sortable searchable">email</th>
                <th></th>
                <th></th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var stu in Model)
            {
                var id = stu.Id.StartsWith(prefix) ? stu.Id.Substring(prefix.Length) : stu.Id;
                <tr asp-controller="Users" asp-action="Detail" asp-route-uid="@stu.UserId" class="@(stu.Email == null && stu.UserName != null ? "table-warning" : "")" role="row" data-toggle="gotoUrl">
                    <td>@id</td>
                    <td>@stu.Name</td>
                    <td class="@(stu.UserName != null ? "" : "disabled")" use-a>@stu.UserName</td>
                    <td class="@(stu.IsVerified.GetValueOrDefault() ? "" : "ignore")" use-a>@stu.Email</td>
                    <td><a asp-show-if="@(stu.UserName != null && !stu.IsVerified.GetValueOrDefault())" asp-action="MarkVerified" asp-route-stuid="@id" asp-route-uid="@stu.UserId" asp-route-page="@Model.CurrentPage" title="mark this student verified" data-toggle="ajaxWindow" data-target="signStud"><i class="fas fa-fingerprint"></i></a></td>
                    <td><a asp-show-if="stu.UserName != null" asp-action="Unlink" asp-route-stuid="@id" asp-route-uid="@stu.UserId" asp-route-page="@Model.CurrentPage" title="unlink this student with account" data-toggle="ajaxWindow" data-target="unlinkStud"><i class="fas fa-unlink"></i></a></td>
                    <td><a asp-action="Delete" asp-route-stuid="@id" asp-route-page="@Model.CurrentPage" title="delete this student" data-toggle="ajaxWindow" data-target="deleteStud"><i class="fas fa-trash-alt"></i></a></td>
                </tr>
            }
        </tbody>
    </table>
</div>

<script>
    $().ready(function () {
        $('.data-table').DataTable({
            "paging": false,
            "searching": true,
            "ordering": true,
            "order": [[0, 'asc']],
            "info": false,
            "autoWidth": true,
            "language": {
                "searchPlaceholder": "filter table",
                "search": "_INPUT_",
            },
            'aoColumnDefs': [
                { aTargets: ['sortable'], bSortable: true },
                { aTargets: ['searchable'], bSearchable: true },
                { aTargets: ['_all'], bSortable: false, bSearchable: false }
            ],
        });

        $('.dataTables_wrapper .row:first-child .col-md-6:first-child').html($('#paging').html());
        $('.dataTables_wrapper .pagination-sm').addClass('mb-0');
    });
</script>

<p>
    <a asp-action="Add" class="btn btn-primary" data-toggle="ajaxWindow" data-target="addStud" title="add students"><i class="fas fa-plus"></i>&nbsp;Add</a>
    <a asp-action="Switch" asp-controller="Tenants" asp-route-returnUrl="@Context.Request.Path.Value" class="btn btn-secondary" title="switch tenant"><i class="fas fa-globe-asia"></i>&nbsp;Switch tenant</a>
</p>
